//
//  EmployeeTimeViewController.swift
//  HuaYun
//
//  Created by enghou on 2019/1/11.
//  Copyright © 2019 xyxorigation. All rights reserved.
//

import UIKit

class EmployeeTimeViewController: RefreshedTableViewController,SearchCondition {

    var selectProjName = ""
    var name = ""
    var date = ""
    
    func search(with orgcode: String?, year: Int?, projname: String?) {
        
    }
    
    func search(with orgc: String?, yearmonth: String?, projname: String?) {
        if (orgc?.count ?? 0) > 0 {
            self.name = orgc ?? ""
        }
        
        if (yearmonth?.count ?? 0) > 0 {
            self.date = yearmonth  ?? ""
        }
        
        if (projname?.count ?? 0) > 0 {
            self.selectProjName = projname ?? ""
        }
        
        self.presenting = false
        
        let searchButton = UIBarButtonItem.init(image: UIImage(named: "icon_nav_search2")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(searchAct))
        self.navigationItem.rightBarButtonItem = searchButton
        
        self.beginRefresh()
    }
    
    
    
    override func reset() {
        self.name = ""
        self.selectProjName = ""
        self.date = String(Date().dateLineStr.prefix(7))
        self.beginRefresh()
        let searchButton = UIBarButtonItem.init(image: UIImage(named: "icon_nav_search2")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(searchAct))
        self.navigationItem.rightBarButtonItem = searchButton
//        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.presentedViewController?.dismiss()
        self.presenting = false
        self.presenting = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.hidesBackButton = true
        let back = UIBarButtonItem.init(image: UIImage.init(named: "icon_nav_whiteback")!.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(popobjc))
        back.tintColor = .white
        self.navigationItem.leftBarButtonItem = back
        self.tableView.separatorInset = .zero
        self.tableView.register(UINib.init(nibName: "EmployeeTimeCell", bundle: Bundle.main), forCellReuseIdentifier: "EmployeeTimeCell")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "icon_nav_search2")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(searchAct))
        self.date = String(Date().dateLineStr.prefix(7))
        // Do any additional setup after loading the view.
    }
    
    var presenting = false
    
    @objc func searchAct(){
        if self.presenting == false {
            let button = UIBarButtonItem.init(title: "取消", style: .plain, target: self, action: #selector(searchAct))
            self.navigationItem.rightBarButtonItem = button
            let search = EmployeeTimeSearchViewController()
            search.delegate = self
            search.username = self.name
            search.projectname = self.selectProjName
            search.date = self.date
            self.presenting = true
            self.present(vc: search)
        } else {
            let searchButton = UIBarButtonItem.init(image: UIImage(named: "icon_nav_search2")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(searchAct))
            self.navigationItem.rightBarButtonItem = searchButton
//            self.navigationController?.navigationBar.tintColor = UIColor.white
            self.presentedViewController?.dismiss()
            self.presenting = false
        }
    }
    
    override func path(for name: String) -> String {
        return EmployeeWorkTimeAnalysis
    }
    
    override func param(for name: String) -> Dictionary<String, Any> {
        var param = super.param(for: name)
        if self.date.count > 0 {
            param["date"] = self.date
        }
        if self.selectProjName.count > 0 {
            param["projectName"] = self.selectProjName
        }
        if self.name.count > 0 {
            param["name"] = self.name
        }
        return param
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let head = UIView()
        head.backgroundColor = UIColor.tableSeparator
        let stack = UIStackView.init()
        stack.axis = .horizontal
        stack.alignment = .fill
        stack.distribution = .fillEqually
        stack.spacing = 0.5
        head.addSubview(stack)
        stack.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-0.5)
            make.top.equalToSuperview()
        }
        let titles = ["姓名","用工类型","月份","已审批工时","未审批工时"]
        for i in 0 ..< titles.count {
            let label = UILabel()
            label.backgroundColor = UIColor.white
            label.text = titles[i]
            label.textColor = UIColor.hex("191919")
            label.font = UIFont.systemFont(ofSize: 14)
            label.textAlignment = .center
            stack.addArrangedSubview(label)
        }
        return head
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "EmployeeTimeCell")!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 75
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if let cel = cell as? EmployeeTimeCell {
            cel.model = self.source[indexPath.row]
        }
    }
    
}
